package com.yupi.springbootinit.controller;

import cn.hutool.json.JSONUtil;
import com.yupi.springbootinit.common.BaseResponse;
import com.yupi.springbootinit.common.ErrorCode;
import com.yupi.springbootinit.common.ResultUtils;
import com.yupi.springbootinit.exception.BusinessException;
import com.yupi.springbootinit.model.dto.picture.PictureRequest;
import com.yupi.springbootinit.model.entity.Picture;
import com.yupi.springbootinit.service.PictureService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: yudaxian
 * @Description: TODO
 * @DateTime: 2024/10/8 16:44
 **/
@RestController
@RequestMapping("/picture")
public class PictureController {

    @Resource
    private PictureService pictureService;

    @PostMapping("/get")
    public BaseResponse getPictureList(@RequestBody PictureRequest pictureRequest) {
        Integer page = pictureRequest.getPageNum();
        Integer pageSize = pictureRequest.getPageSize();
        String search = pictureRequest.getSearch();

        if (StringUtils.isBlank(search) || ObjectUtils.isEmpty(page) || ObjectUtils.isEmpty(pageSize)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        List<Picture> pictures = this.pictureService.getPicture(pictureRequest);


        Map<String, Object> result = new HashMap<>();
        result.put("total", pictures.size());
        result.put("records", pictures);
        result.put("pageNum", pictureRequest.getPageNum() / 10 + 1);
        result.put("pageSize", pictureRequest.getPageSize());

        return ResultUtils.success(result);
    }

}
